<?php

namespace App\Jobs;


use App\Libraries\Easemob\Easemob;
use App\Models\Msg\MsgComment;
use JMessage\IM\Message;
use JMessage\JMessage;

class SendCommentMsgJob extends Job
{


    private $post_id;
    private $from_id;
    private $to_ids;
    private $comment_id;
    private $main_comment_id;
    private $msg;
    private $type;


    public function __construct($from_id, $post_id, array $to_ids, $comment_id, $main_comment_id, $msg, $type)
    {
        $this->post_id = $post_id;
        $this->from_id = $from_id;
        $this->to_ids = $to_ids;
        $this->comment_id = $comment_id;
        $this->main_comment_id = $main_comment_id;
        $this->msg = $msg;
        $this->type = $type;//type 0-普通消息 1-红包信息 2-VIP过期信息 3-auth改变
    }

    /**
     * Execute the job.
     *
     * @param MsgComment $msgComment
     * @return bool
     * @throws \Exception
     */
    public function handle(MsgComment $msgComment)
    {
        if(!is_array($this->to_ids)){
            return false;
        }
        $client = new JMessage(env('JMSG_APPKEY'), env('JMSG_SECRET'));
        $message = new Message($client);
        foreach($this->to_ids as $v){
            $msgComment->from_id = $this->from_id;
            $msgComment->to_id = $v;
            $msgComment->post_id = $this->post_id;
            $msgComment->comment_id = $this->comment_id;
            $msgComment->main_comment_id = $this->main_comment_id;
            $msgComment->message = $this->msg;
            $msgComment->save();
            $msgComment = new MsgComment();

            $message->sendText(1, [
                'type' => 'admin',
                'id'   => 'beboy_4',
                'name' => '评论'
            ], [
                'type' => 'single',
                'id'   => 'beboy_'.$v,
            ], [
                'text' => $this->msg,
                'extras' => ['user_id'=>4, 'type'=>$this->type]
            ], [
                'notifiable' => false
            ], [
                'offline' => true
            ]);
        }
//        $data = Easemob::singleton()->sendText(4, 'users', $this->to_ids, $this->msg, ['user_id'=>4, 'type'=>$this->type]);
//        if(!empty($data['error'])){
//            throw new \Exception('send comment msg Fail');
//        }
        return true;
    }
}
